package com.googlecode.roleaccesscontrol.uc.user;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.googlecode.roleaccesscontrol.entity.User;
import com.googlecode.roleaccesscontrol.wicket.BasePage;
import com.googlecode.roleaccesscontrol.wicket.common.YesNoListChoice;

public class UserAddPage extends BasePage {

	@SpringBean
	protected UserRepository repository;

	public UserAddPage() {
		Form<User> form = new Form<User>("form", new CompoundPropertyModel(new User())) {
			@Override
			protected void onSubmit() {
				try {
					repository.save(getModelObject());
					setResponsePage(UserListPage.class);
				} catch (RuntimeException e) {
					System.out.println("---------------------------------------------");
					e.printStackTrace();
					System.out.println("=============================================");
					throw e;
				}
			}
		};
		add(form);

		form.add(new TextField("name"));
		form.add(new TextField("fullName"));
		form.add(new YesNoListChoice("active").setNullValid(false));
	}

	@Override
	protected String getSuffixTitleString() {
		return getLocalizer().getString("page.title", this);
	}

}
